package com.meida.module.bus.provider.service.impl;

import cn.hutool.core.util.NumberUtil;
import cn.hutool.log.StaticLog;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.meida.common.mybatis.base.service.impl.BaseServiceImpl;
import com.meida.common.mybatis.entity.EntityMap;
import com.meida.common.mybatis.model.ResultBody;
import com.meida.common.mybatis.query.CriteriaQuery;
import com.meida.common.mybatis.query.CriteriaSave;
import com.meida.common.security.OpenHelper;
import com.meida.common.security.OpenUser;
import com.meida.module.bus.client.entity.*;
import com.meida.module.bus.provider.api.soap.SyncSapUtil;
import com.meida.module.bus.provider.api.util.ApiConstants;
import com.meida.module.bus.provider.api.util.PmsApiUtil;
import com.meida.module.bus.provider.mapper.BusReceiptDeliveryRecordMapper;
import com.meida.module.bus.provider.mapper.BusReceiptDeliveryReqMapper;
import com.meida.module.bus.provider.service.BusMaterialPlan100Service;
import com.meida.module.bus.provider.service.BusMaterialPlan200Service;
import com.meida.module.bus.provider.service.BusMaterialService;
import com.meida.module.bus.provider.service.BusRecDeliReqConfService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 预留发货记录表接口实现类
 *
 * @author flyme
 * @date 2020-12-02
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class BusRecDeliReqConfServiceImpl extends BaseServiceImpl<BusReceiptDeliveryRecordMapper, BusReceiptDeliveryRecord> implements BusRecDeliReqConfService {

    @Autowired
    private PmsApiUtil pmsApiUtil;

    @Autowired
    private BusMaterialPlan100Service plan100Service;

    @Autowired
    private BusMaterialPlan200Service plan200Service;

    @Autowired
    private BusMaterialService busMaterialService;

    @Override
    public ResultBody beforeAdd(CriteriaSave cs, BusReceiptDeliveryRecord brdr, EntityMap extra) {
        return ResultBody.ok();
    }

    @Override
    @Transactional(propagation = Propagation.NOT_SUPPORTED, readOnly = true)
    public ResultBody beforePageList(CriteriaQuery<BusReceiptDeliveryRecord> cq, BusReceiptDeliveryRecord brdr, EntityMap requestMap) {
        cq.notIn("syncStatus", ApiConstants.REC_DELIVERY_CANCEL, ApiConstants.REC_DELIVERY_COMMIT, ApiConstants.REC_DELIVERY_FINISH);
        cq.like(BusReceiptDeliveryRecord.class, "status");
        cq.like(BusReceiptDeliveryRecord.class, "projectCode");
        cq.like(BusReceiptDeliveryRecord.class, "projectName");
        cq.like(BusReceiptDeliveryRecord.class, "majorCode");
        cq.like(BusReceiptDeliveryRecord.class, "majorName");
        cq.like(BusReceiptDeliveryRecord.class, "packageNo");
        cq.like(BusReceiptDeliveryRecord.class, "packageName");
        cq.like(BusReceiptDeliveryRecord.class, "userName");
        cq.like(BusReceiptDeliveryRecord.class, "appTime");
        cq.like(BusReceiptDeliveryRecord.class, "receivedDate");
        cq.like(BusReceiptDeliveryRecord.class, "zid");
        cq.like(BusReceiptDeliveryRecord.class, "zitem");
        cq.like(BusReceiptDeliveryRecord.class, "budat");
        cq.like(BusReceiptDeliveryRecord.class, "bldat");
        cq.like(BusReceiptDeliveryRecord.class, "bktxt");
        cq.like(BusReceiptDeliveryRecord.class, "supplierCode");
        cq.like(BusReceiptDeliveryRecord.class, "supplierName");
        cq.like(BusReceiptDeliveryRecord.class, "reserveNo");
        cq.like(BusReceiptDeliveryRecord.class, "reserveRow");
        cq.like(BusReceiptDeliveryRecord.class, "lgort");
        cq.like(BusReceiptDeliveryRecord.class, "msgtyFromSap");
        cq.like(BusReceiptDeliveryRecord.class, "msgtxFromSap");
        cq.like(BusReceiptDeliveryRecord.class, "mblnrFromSap");
        cq.like(BusReceiptDeliveryRecord.class, "mblppoFromSap");
        cq.like(BusReceiptDeliveryRecord.class, "mjahrFromSap");
        List<String> majorCodes = pmsApiUtil.getAuthMajorCodes(OpenHelper.getUserId());
        if(null != majorCodes) {
            cq.in("majorCode", majorCodes);
        }
        List<String> projectCodeList = pmsApiUtil.getAuthProjectIds(OpenHelper.getUserId());
        if(null != projectCodeList) {
            cq.in("projectCode", projectCodeList);
        }
        return ResultBody.ok();
    }

//    @Override
//    public Boolean syncSap(List<BusReceiptDeliveryRecord> records) throws Exception {
//        Boolean rs = Boolean.FALSE;
//        if (pmsApiUtil.sapConnectionForRecCheck()) {
//            StaticLog.debug("SAP 接口通信开关为关闭。");
//            return rs;
//        }
//        Map<String, String> rsMap = SyncSapUtil.syncSap(records, 0);
//        records.stream().forEach(recordObj -> {
//            if (ApiConstants.REC_DERVERY_SAP_BACK_ERR.equals(rsMap.get(ApiConstants.MAP_MSGTYFROMSAP))) {
//                recordObj.setSyncStatus(ApiConstants.REC_DELIVERY_ERR);
//                recordObj.setMsgtyFromSap(ApiConstants.REC_DERVERY_SAP_BACK_ERR);
//                recordObj.setMsgtxFromSap(rsMap.get(ApiConstants.MAP_MSGTXFROMSAP));
//            } else {
//                recordObj.setSyncStatus(ApiConstants.REC_DELIVERY_FINISH);
//                recordObj.setZid(recordObj.getRecDecId());
//                recordObj.setZitem(1);
//            }
//        });
//        if (!ApiConstants.REC_DERVERY_SAP_BACK_ERR.equals(rsMap.get(ApiConstants.MAP_MSGTYFROMSAP))) {
//               rs = Boolean.TRUE;
//        }
//        return rs;
//    }
//
//    @Override
//    public void reqCancel(Long[] selectIds) {
//        List<Long> selectIdsList = Arrays.asList(selectIds);
//        List<BusReceiptDeliveryRecord> recordCancels = Lists.newArrayList();
//        OpenUser user = OpenHelper.getUser();
//        Collection<BusReceiptDeliveryRecord> busReceiptDeliveryRecords = this.listByIds(selectIdsList);
//        busReceiptDeliveryRecords.stream().forEach(updObj -> {
//            updObj.setConfUserId(user.getUserId());
//            updObj.setConfUserName(user.getUsername());
//            updObj.setSyncStatus(ApiConstants.REC_DELIVERY_CANCEL);
//            updObj.setDelFlg(ApiConstants.DEL_ON);
//            recordCancels.add(updObj);
//        });
//        if (0 < recordCancels.size()) {
//            this.updateBatchById(recordCancels);
//        }
//    }
}
